package net.googlecode.demenkov.task4.utils;

import net.googlecode.demenkov.task4.source.StringUtil;

import java.util.StringTokenizer;

/**
 * StringUtil that reverse all letters in all words and log this action.
 *
 * @author Yura Demenkov
 * @since 04.04.11
 */
public class ReverseWords extends StringUtil {
    /**
     * Constructor that create StringUtil and assosiate it with inputstring.
     *
     * @param inputstring
     */
    public ReverseWords(String inputstring) {
        super(inputstring);
    }

    /**
     * Reverse all letters in all words in inputstring and log it.
     *
     * @return Corrected string.
     */
    public String ReverseCharsInWords() {
        boolean isDot = false;
        String outputString = new String();
        if ((inputString.charAt(inputString.length() - 1)) == '.') {
            inputString = inputString.substring(0, inputString.length() - 1);
            isDot = true;
        }
        StringTokenizer st = new StringTokenizer(inputString, " ", true);
        StringBuilder sb = new StringBuilder();
        while (st.hasMoreTokens()) {
            String token = st.nextToken();
            StringBuilder reversetoken = new StringBuilder();
            for (int i = token.length() - 1; i >= 0; i--) {
                reversetoken.append(token.charAt(i));
            }
            sb.append(reversetoken.toString());
        }
        if (isDot) sb.append('.');
        outputString = sb.toString();
        this.LogMessage(outputString, "Reverse letters in every word.");
        return outputString;
    }

}
